const { EventEmitter } =  require('./events')

const { createServer } = require('http')
const events = require("node:events");
let port = 8080
const eventEmitter = new EventEmitter()
const app = createServer()

eventEmitter.once('connect', handlerConnect)
eventEmitter.once('close', handlerClose)
eventEmitter.on('portChange', handlerPortChange)
// eventEmitter.removeListeners('portChange', handlerPortChange)
eventEmitter.removeAllListeners('portChange', handlerPortChange)


app.listen(port, () => eventEmitter.emit('connect', port))

setTimeout(() => {
    app.close(() => eventEmitter.emit('close'))
},2000)


setTimeout(() => {
    eventEmitter.emit('portChange', port)
}, 3000)
function handlerConnect(port) {
    console.log(`Server is running on port ${port}`)
}

function handlerClose(){
    console.log('Server is closed')
}

function handlerPortChange(port) {
    app.close()
    port = 5050
    app.listen(port, () => console.log(`Server is changed onto port ${port}`))
}
